//
//  Setup.m
//  ShoppingCompare
//
//  Created by Wenjie on 11-6-12.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Setup.h"
#import "RootViewController.h"
#import "Order.h"
#import "MinPrice.h"
#import "MaxPrice.h"

@implementation Setup

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// Navigation set --------------------------------------
	self.title = @"Advance Setup";
	self.view.backgroundColor = [UIColor whiteColor];
	
	// First line --------------------------------------
	UILabel* lblTrans = [[[UILabel alloc] initWithFrame:CGRectMake(40.0f, 50.0f, 100.0f, 40.0f)] autorelease];
	[lblTrans setFont:[UIFont systemFontOfSize:17.0]];
	lblTrans.text = @"Auto trans.";
	swhTrans = [[[UISwitch alloc] initWithFrame:CGRectMake(170.0f, 55.0f, 100.0f, 40.0f)] autorelease];
	[swhTrans addTarget:self action:@selector(changeTrans) forControlEvents:UIControlEventValueChanged];
	if (trans) {
		[swhTrans setOn:YES];
	} else {
		[swhTrans setOn:NO];
	}
	
	// Second line --------------------------------------
	UILabel* lblOrder = [[[UILabel alloc] initWithFrame:CGRectMake(40.0f, 120.0f, 100.0f, 40.0f)] autorelease];
	[lblOrder setFont:[UIFont systemFontOfSize:17.0]];
	lblOrder.text = @"Order by";
	btnOrder = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	btnOrder.frame = CGRectMake(170.0f, 120.0f, 98.0f, 40.0f);
	[btnOrder addTarget:self action:@selector(selOrder) forControlEvents:UIControlEventTouchUpInside];
	
	// Third line --------------------------------------
	UILabel* lblMin = [[[UILabel alloc] initWithFrame:CGRectMake(40.0f, 190.0f, 100.0f, 40.0f)] autorelease];
	[lblMin setFont:[UIFont systemFontOfSize:17.0]];
	lblMin.text = @"Min. price";
	btnMin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	btnMin.frame = CGRectMake(170.0f, 190.0f, 98.0f, 40.0f);
	[btnMin addTarget:self action:@selector(selMin) forControlEvents:UIControlEventTouchUpInside];
	
	// Forth line --------------------------------------
	UILabel* lblMax = [[[UILabel alloc] initWithFrame:CGRectMake(40.0f, 260.0f, 100.0f, 40.0f)] autorelease];
	[lblMax setFont:[UIFont systemFontOfSize:17.0]];
	lblMax.text = @"Max. price";
	btnMax = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	btnMax.frame = CGRectMake(170.0f, 260.0f, 98.0f, 40.0f);
	[btnMax addTarget:self action:@selector(selMax) forControlEvents:UIControlEventTouchUpInside];
	
	// Add to view --------------------------------------
	[self.view addSubview:lblTrans];
	[self.view addSubview:swhTrans];
	[self.view addSubview:lblOrder];
	[self.view addSubview:btnOrder];
	[self.view addSubview:lblMin];
	[self.view addSubview:btnMin];
	[self.view addSubview:lblMax];
	[self.view addSubview:btnMax];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
	switch (order) {
		case 0:
			[btnOrder setTitle:@"DEFA." forState:UIControlStateNormal];
			break;
		case 1:
			[btnOrder setTitle:@"ASC." forState:UIControlStateNormal];
			break;
		default:
			[btnOrder setTitle:@"DESC." forState:UIControlStateNormal];
			break;
	}
	if (minP < 0) {
		[btnMin setTitle:@"Un Set" forState:UIControlStateNormal];
	} else {
		[btnMin setTitle:[NSString stringWithFormat:@"%ld", minP] forState:UIControlStateNormal];
	}
	if (maxP < 0) {
		[btnMax setTitle:@"Un Set" forState:UIControlStateNormal];
	} else {
		[btnMax setTitle:[NSString stringWithFormat:@"%ld", maxP] forState:UIControlStateNormal];
	}
}
// --------------------------------------
- (void)changeTrans {
	trans = swhTrans.on;
}

- (void)selOrder {
	RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
	UINavigationController* navi = [rvc->search navigationController];
	Order* orderBy = [[Order alloc] init];
	[navi pushViewController:orderBy animated:YES];
	
}

- (void)selMin {
	RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
	UINavigationController* navi = [rvc->search navigationController];
	MinPrice* minPrice = [[MinPrice alloc] init];
	[navi pushViewController:minPrice animated:YES];
}

- (void)selMax {
	RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
	UINavigationController* navi = [rvc->search navigationController];
	MinPrice* maxPrice = [[MaxPrice alloc] init];
	[navi pushViewController:maxPrice animated:YES];
}

@end
